﻿using APP.QUANLY.Common;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Text;

namespace APP.QUANLY.Common
{
    public enum RunTypes
    {
        Trial = 0,
        Full,
        Expired,
        UnKnown
    }

    public class TrialLicence
    {
        private string _BaseString;
        private const int nDayLimitDemo = 30;
        private string _SoftwareName = "";
        private string _AppKey = "";
        private string _Guid = "";
        private string _SerialKey = "";
        private string _SerialKeyFull = "";
        private string _RegKeyPath = "";
        public string RegKeyPath { get { return this._RegKeyPath; } set { this._RegKeyPath = value; } }

         public TrialLicence(string SoftwareName, string AppKey, string Guid)
         {
             this._SoftwareName = SoftwareName;
             this._AppKey = AppKey;
             this._Guid = Guid;
         }
         public string SetSerialKey(RunTypes types, bool IsActiveSoftware)
         {
             string Key = "";
             if ((types == RunTypes.Trial || types == RunTypes.Expired) && !IsActiveSoftware)
             {
                 Key = GetSerialKey(GenerateSerialKey());
             }
             
             return Key;
         }
         public string SetLicenceKey(RunTypes types, bool IsActiveSoftware, string SerialKey)
         {
             string Key = "";
             if ((types == RunTypes.Trial || types == RunTypes.Expired) && IsActiveSoftware)
                 Key = SetLicenceKey(SerialKey);
             return Key;
         }
         private string GenerateSerialKey()
         {
             Sys.Utils.UseBaseBoardManufacturer = false;
             Sys.Utils.UseBaseBoardProduct = false;
             Sys.Utils.UseBiosManufacturer = false;
             Sys.Utils.UseBiosVersion = false;
             Sys.Utils.UseDiskDriveSignature = true;
             Sys.Utils.UsePhysicalMediaSerialNumber = false;
             Sys.Utils.UseProcessorID = true;
             Sys.Utils.UseVideoControllerCaption = false;
             Sys.Utils.UseWindowsSerialNumber = false;

             _BaseString = Sys.Utils.GetSerialKey(TCEncryption.ToEncrypt(_SoftwareName));
             _Guid = _Guid.Replace("-", "");
             if (_BaseString.Length > _Guid.Length)
             {
                 _BaseString = _BaseString.Substring(0, _Guid.Length);
             }
             if (_Guid.Length > _BaseString.Length)
             {
                 _Guid = _Guid.Substring(0, _BaseString.Length);
             }
             for (int i = 0; i < _BaseString.Length; i++)
             {
                 _SerialKey += _BaseString[i].ToString() + _Guid[_BaseString.Length - i - 1].ToString();
             }
             foreach (char c in _SerialKey)
             {
                 _SerialKeyFull += this.Convertion(c.ToString()).ToString();
             }
             return _SerialKeyFull;
         }
         private string GetSerialKey(string SerialKey)
         {
             _SerialKeyFull = _SerialKeyFull.Substring(0, 16);
             return _SerialKeyFull;
         }
         public string SetLicenceKey(string SerialKey)
         {
             string LicenceKey = "";
             if (SerialKey == GetSerialKey(GenerateSerialKey()))
             {
                 foreach (Char c in SerialKey)
                 {
                     LicenceKey += this.Unconvertion(c.ToString()).ToString();
                 }
                 return LicenceKey;
             }
             else
             {
                 return "SerialKey không hợp lệ";
             }
         }
         private string Convertion(string KeyConvert)
         {

             switch (KeyConvert.ToString().ToUpper())
             {
                 case "A":
                     KeyConvert = "0";
                     break;
                 case "B":
                     KeyConvert = "1";
                     break;
                 case "C":
                     KeyConvert = "2";
                     break;
                 case "D":
                     KeyConvert = "3";
                     break;
                 case "E":
                     KeyConvert = "4";
                     break;
                 case "F":
                     KeyConvert = "5";
                     break;
                 case "G":
                     KeyConvert = "6";
                     break;
                 case "H":
                     KeyConvert = "7";
                     break;
                 case "I":
                     KeyConvert = "8";
                     break;
                 case "J":
                     KeyConvert = "9";
                     break;
                 case "K":
                     KeyConvert = "a";
                     break;
                 case "L":
                     KeyConvert = "b";
                     break;
                 case "M":
                     KeyConvert = "c";
                     break;
                 case "N":
                     KeyConvert = "d";
                     break;
                 case "O":
                     KeyConvert = "e";
                     break;
                 case "P":
                     KeyConvert = "f";
                     break;
                 case "Q":
                     KeyConvert = "g";
                     break;
                 case "R":
                     KeyConvert = "h";
                     break;
                 case "S":
                     KeyConvert = "i";
                     break;
                 case "T":
                     KeyConvert = "j";
                     break;
                 case "U":
                     KeyConvert = "k";
                     break;
                 case "V":
                     KeyConvert = "l";
                     break;
                 case "W":
                     KeyConvert = "m";
                     break;
                 case "X":
                     KeyConvert = "v";
                     break;
                 case "Y":
                     KeyConvert = "y";
                     break;
                 case "Z":
                     KeyConvert = "z";
                     break;
             }
             return KeyConvert;
         }
         private string Unconvertion(string KeyUnconvert)
         {

             switch (KeyUnconvert.ToString().ToUpper())
             {
                 case "0":
                     KeyUnconvert = "A";
                     break;
                 case "1":
                     KeyUnconvert = "B";
                     break;
                 case "2":
                     KeyUnconvert = "C";
                     break;
                 case "3":
                     KeyUnconvert = "D";
                     break;
                 case "4":
                     KeyUnconvert = "E";
                     break;
                 case "5":
                     KeyUnconvert = "F";
                     break;
                 case "6":
                     KeyUnconvert = "G";
                     break;
                 case "7":
                     KeyUnconvert = "H";
                     break;
                 case "8":
                     KeyUnconvert = "I";
                     break;
                 case "9":
                     KeyUnconvert = "J";
                     break;
                 case "A":
                     KeyUnconvert = "K";
                     break;
                 case "B":
                     KeyUnconvert = "L";
                     break;
                 case "C":
                     KeyUnconvert = "M";
                     break;
                 case "D":
                     KeyUnconvert = "N";
                     break;
                 case "E":
                     KeyUnconvert = "O";
                     break;
                 case "F":
                     KeyUnconvert = "P";
                     break;
                 case "G":
                     KeyUnconvert = "Q";
                     break;
                 case "H":
                     KeyUnconvert = "R";
                     break;
                 case "I":
                     KeyUnconvert = "S";
                     break;
                 case "J":
                     KeyUnconvert = "T";
                     break;
                 case "K":
                     KeyUnconvert = "U";
                     break;
                 case "L":
                     KeyUnconvert = "V";
                     break;
                 case "M":
                     KeyUnconvert = "W";
                     break;
                 case "V":
                     KeyUnconvert = "X";
                     break;
                 case "Y":
                     KeyUnconvert = "Y";
                     break;
                 case "Z":
                     KeyUnconvert = "Z";
                     break;
             }
             return KeyUnconvert;
         }
         //public void CreateKeyInRegister(string KeyName, string Values)
         //{
         //    int Key = -2147483646;
         //    string text = this._RegKeyPath;
         //    int hKey = 0;
         //    int num = TrialLicence.RegCreateKey(Key, ref text, ref hKey);
         //    string expression = Values;
         //    num = TrialLicence.RegSetValueExString(hKey, ref KeyName, 0, 1, ref expression, Strings.Len(expression));
         //    num = TrialLicence.RegCloseKey(hKey);
         //}
         //public string GetKeyInRegister(string KeyName)
         //{
         //    int arg_13_0 = -2147483646;
         //    string text = this._RegKeyPath;
         //    int num2 = 0;
         //    int num = TrialLicence.RegOpenKeyEx(arg_13_0, ref text, 0, 1, ref num2);
         //    bool flag = num != 0;
         //    checked
         //    {
         //        string result;
         //        if (flag)
         //        {
         //            result = "";
         //        }
         //        else
         //        {
         //            string text2 = new string(' ', 255);
         //            int arg_56_0 = num2;
         //            int arg_56_2 = 0;
         //            int num3 = 0;
         //            int num4 = Strings.Len(text2);
         //            num = TrialLicence.RegQueryValueEx(arg_56_0, ref KeyName, arg_56_2, ref num3, ref text2, ref num4);
         //            TrialLicence.RegCloseKey(num2);
         //            flag = (num != 0);
         //            if (flag)
         //            {
         //                result = "";
         //            }
         //            else
         //            {
         //                string text3 = "";
         //                int arg_8B_0 = 0;
         //                int num5 = text2.Length - 1;
         //                int num6 = arg_8B_0;
         //                while (true)
         //                {
         //                    int arg_D1_0 = num6;
         //                    int num7 = num5;
         //                    if (arg_D1_0 > num7)
         //                    {
         //                        break;
         //                    }
         //                    string text4 = text2.Substring(num6, 1);
         //                    int num8 = Strings.Asc(text4);
         //                    flag = (num8 != 0);
         //                    if (!flag)
         //                    {
         //                        break;
         //                    }
         //                    text3 += text4;
         //                    num6++;
         //                }
         //                result = Strings.Trim(text3);
         //            }
         //        }
         //        return result;
         //    }
         //}

        

         //public static bool CheckRegClasskey()
         //{
         //    bool isReg = ModReg.m_isReg;
         //    bool result = false;
         //    if (isReg)
         //    {
         //        bool flag = Operators.CompareString(ModReg.sKey, "", false) == 0;
         //        if (flag)
         //        {
         //            ModReg.sKey = clsu.GetKeyInRegister("VSBMSNET_REGCLASSKEY");
         //        }
         //        flag = (Operators.CompareString(ModReg.sKeyApp, ModReg.sKey, false) == 0);
         //        if (flag)
         //        {
         //            result = true;
         //        }
         //        else
         //        {
         //            FrmRegClass frmRegClass = new FrmRegClass();
         //            flag = frmRegClass.ShowDialog("");
         //            if (flag)
         //            {
         //                result = true;
         //            }
         //        }
         //    }
         //    else
         //    {
         //        result = true;
         //    }
         //    return result;
         //}

         //public static bool CheckToMonthYearOver(int iMonth, int iYear)
         //{
         //    if (iMonth > 12)
         //    {
         //        iMonth = 12;
         //    }
         //    if (iMonth < 1)
         //    {
         //        iMonth = 1;
         //    }
         //    if (iYear < 1900)
         //    {
         //        iYear = 1900;
         //    }
         //    if (iYear > 2050)
         //    {
         //        iYear = 2050;
         //    }
         //    DateTime time = Conversions.ToDate(iYear.ToString() + "-" + iMonth.ToString() + "-1");
         //    int num = (int)DateAndTime.DateDiff(DateInterval.Month, time, DateAndTime.Now, FirstDayOfWeek.System, FirstWeekOfYear.System);
         //    if (num >= 0)
         //    {
         //        Interaction.MsgBox("Thời gian dùng thử đã hết, xin vui lòng liên hệ tới số (0128.289.2832)", MsgBoxStyle.Critical, null);
         //        return false;
         //    }
         //    return true;
         //}
    }
}
